<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp "&#x00A0;">
]>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  xmlns:site.library="urn:site.library"
  xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
  exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets site.library">
  <xsl:import href="_common.xslt"/>

  <xsl:output method="xml" omit-xml-declaration="yes" indent="no"/>

  <xsl:param name="currentPage" />
  <xsl:variable name="listName" select="string(/macro/listName)"/>
  <xsl:variable name="selPagesIds" select="$currentPage/ancestor-or-self::*/@id"/>


  <xsl:variable name="SiteInfo" select="site.library:PageSite($currentPage/@id)"/>
  <xsl:variable name="allListsLocation" select="site.library:GetLinkLists($SiteInfo/node/@id)"/>

  <xsl:variable name="isEditMode" select="site.library:IsCanvas()"/>

  <xsl:template match="/">    
      <!-- find list node and render it-->
      <xsl:choose>
        <xsl:when test="string-length($allListsLocation)>0">
          <xsl:variable name="allLists" select="umbraco.library:GetXmlNodeById($allListsLocation)"/>
          <xsl:choose>
            <xsl:when test="string-length($listName)>0">
              <!--render list by name-->
              <xsl:variable name="listNode" select="$allLists/*[@isDoc and @nodeName=$listName]"/>
              <xsl:call-template name="RenderLinksList">
                <xsl:with-param name="ListNode" select="$listNode"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <!--render 1 list-->
              <xsl:call-template name="RenderLinksList">
                <xsl:with-param name="ListNode" select="$allLists/*[@isDoc and position()=1]"/>
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="comment">
            <xsl:with-param name="text" select="'site hasnt linkslist in settings,create them'"/>
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
  </xsl:template>

  <xsl:template name="RenderLinksList">
    <xsl:param name="ListNode"/>
    <xsl:if test="$ListNode">
      <xsl:if test="string-length($ListNode/title)>0"><h3><xsl:value-of disable-output-escaping="yes" select="$ListNode/title"/></h3></xsl:if>
      <xsl:if test="string-length($ListNode/shortContent)>0"><xsl:value-of disable-output-escaping="yes" select="$ListNode/shortContent"/></xsl:if>
      <ul class="llist_container{site.library:ShortIf($isEditMode,' linkslist_editor','')}">
        <xsl:if test="$isEditMode">
          <xsl:attribute name="onclick">javascript:showEditNodeDialog( '<xsl:value-of disable-output-escaping="yes" select="$ListNode/@id"/>', '<xsl:value-of disable-output-escaping="yes" select="$ListNode/@nodeName"/>');</xsl:attribute>
        </xsl:if>
      <xsl:choose>
        <xsl:when test="count($ListNode/dataSource/nodes/*)>0">
            <xsl:for-each select="umbraco.library:GetXmlNodeById($ListNode/dataSource/nodes/*[1]/@id)/*[@isDoc and string(hideInMenu) != '1']">
              <xsl:call-template name="RenderLinkItem">
                <xsl:with-param name="Page" select="."/>
                <xsl:with-param name="ListNode" select="$ListNode"/>
              </xsl:call-template>
            </xsl:for-each>
        </xsl:when>
        <xsl:when test="count($ListNode/staticLinks/nodes/*)>0">
          <xsl:for-each select="$ListNode/staticLinks/nodes/*">
            <xsl:call-template name="RenderLinkItem">
              <xsl:with-param name="Page" select="umbraco.library:GetXmlNodeById(./@id)"/>
              <xsl:with-param name="ListNode" select="$ListNode"/>
            </xsl:call-template>
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise>
            <xsl:for-each select="$ListNode/*[@isDoc]">
              <xsl:call-template name="RenderLinkItem">
                <xsl:with-param name="Page" select="current()"/>
                <xsl:with-param name="ListNode" select="$ListNode"/>
              </xsl:call-template>
            </xsl:for-each>
        </xsl:otherwise>
      </xsl:choose>
      </ul>
    </xsl:if>
  </xsl:template>

  <xsl:template name="RenderLinkItem">
    <xsl:param name="Page" select="false"/>
    <xsl:param name="ListNode" />
    
    <xsl:variable name="isSelected" select="count($selPagesIds[number(.)=number($Page/@id)])>0"/>
    <xsl:variable name="itemUrl"><xsl:choose><xsl:when test="$Page/navigateUrl and $Page/navigateUrl != ''"><xsl:value-of select="$Page/navigateUrl"/></xsl:when><xsl:otherwise><xsl:value-of select="umbraco.library:NiceUrl($Page/@id)"/></xsl:otherwise></xsl:choose></xsl:variable>
    <xsl:variable name="exClass"><xsl:if test ="$isSelected"> active</xsl:if><xsl:if test="position()=1"> first</xsl:if><xsl:if test="position()=last()"> last</xsl:if></xsl:variable>
    <xsl:if test="position()!=1"><li><span>|</span></li></xsl:if>
    <li class="blinksitem {$exClass}">
      <a href="{$itemUrl}">
        <xsl:if test="$isEditMode">
          <xsl:attribute name="onclick">javascript:showEditNodeDialog( '<xsl:value-of disable-output-escaping="yes" select="$ListNode/@id"/>', 'Editing link list settings: <xsl:value-of disable-output-escaping="yes" select="$ListNode/@nodeName"/>');</xsl:attribute>
        </xsl:if>
        <xsl:if test="string($Page/target) != ''"><xsl:attribute name="target"><xsl:value-of select="$Page/target"></xsl:value-of></xsl:attribute></xsl:if>
        <xsl:if test="string($Page/menuImage) != ''"><img alt="{$Page/@nodeName}" src="{umbraco.library:GetMedia($Page/menuImage, 0)/umbracoFile}"/></xsl:if>
        <xsl:if test="not (string($Page/menuImageOnly) = '1')">
          <!--show [title] or [menu title]-->
          <xsl:choose>
            <xsl:when test="string($Page/menuTitle) != ''"><xsl:value-of select="$Page/menuTitle" disable-output-escaping="yes"/></xsl:when>
            <xsl:otherwise><xsl:value-of select="$Page/title" disable-output-escaping="yes"/></xsl:otherwise>
          </xsl:choose>
        </xsl:if>
      </a>
    </li>
  </xsl:template>


</xsl:stylesheet>